<template>
  <div class="login-container">
    <div class="background-image"></div>

    <div class="form-container">
      <h1 class="system-title">医疗门诊预约系统</h1>

      <el-form :model="loginForm" ref="loginForm" :rules="loginRules" label-width="80px" class="login-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-radio-group v-model="loginForm.role">
          <el-radio label="doctor">医生</el-radio>
          <el-radio label="admin">管理员</el-radio>
          <el-radio label="patient">患者</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
        <el-button @click="handleRegister">注册</el-button>
      </el-form-item>
    </el-form>

      <!-- Register Drawer -->
      <el-drawer :visible.sync="registerDrawerVisible" title="注册" size="30%" :before-close="beforeRegisterDrawerClose">
        <el-form :model="registerForm" ref="registerForm" :rules="registerRules" label-width="80px" class="register-form">
          <!-- Your existing registration form -->
          <el-form-item label="姓名" prop="name">
            <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="registerForm.age" placeholder="请输入年龄"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="registerForm.sex">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="registerForm.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="registerForm.address" placeholder="请输入地址"></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="role">
            <el-radio-group v-model="registerForm.role">
              <el-radio label="patient">患者</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleRegisterSubmit">提交</el-button>
            <el-button @click="closeRegisterDrawer">取消</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  position: relative;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('../assets/4.jpg') center/cover;
  opacity: 0.5; /* Adjust the opacity as needed */
  /* Ensure the background is behind the form */
}

.form-container {
  position: relative;
 
  width: 310px;
  background-color: rgba(255, 255, 255, 0.8); /* Adjust background color and opacity as needed */
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Add a subtle shadow for depth */
}

.system-title {
  font-size: 24px;
  text-align: center;
  margin-bottom: 20px;
}

/* Your existing styles for login-form and register-form */
.login-form {
  width: 100%;
}

.register-form {
  width: 100%;
}
</style>


<script>
import axios from 'axios'
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        role: '' // 新增角色字段
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      },
      registerDrawerVisible: false,
      registerForm: {
        name: '',
        password: '',
        age: '',
        phone: '',
        address: '',
        role: '',
        sex: '' // Added gender field
      },
      registerRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    ...mapMutations(['setUser']),
    handleLogin() {

  const { username, password, role } = this.loginForm;
if(role==='admin'||role==="patient"){
  axios.post('http://127.0.0.1:3000/api/login', { name: username, password, role })
    .then(response => {
      // Handle successful login
      const userData = response.data.user;
      this.setUser(userData);
       if(userData.role==='admin'){
        this.$router.push('/admin/hospital')
      }else if(userData.role==='patient'){
        this.$router.push('/useradmin/userhome')
      }
      this.$message.success('Login successful');
      
    })
    .catch(error => {
      // Handle login error
      console.error('Login error:', error);
      this.$message.error('Invalid credentials. Please try again.');
    });
}else{
  axios.post('http://127.0.0.1:3000/api/logindoctor', this.loginForm)
        .then(response => {
          // 登录成功
          console.log(response.data.user);
         
          this.setUser( response.data.user);
          this.$router.push('/doctoradmin/checklist')
        })
        .catch(error => {
          // 登录失败
          console.error('登录失败');
          // 在这里可以处理登录失败后的逻辑，例如显示错误信息
        });
    

}
  // Assuming you have Axios installed and imported

},
    handleRegister() {
      this.registerDrawerVisible = true;
      
    },
    
    handleRegisterSubmit() {
      // Validate the registration form
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          // Registration form is valid, proceed with registration logic

          // Use Axios to send a POST request to your registration API endpoint
          axios.post('http://127.0.0.1:3000/api/register', this.registerForm)
            .then(response => {
              // Registration successful, you can handle the response if needed
              this.$message.success('Registration successful');
              // Close the registration drawer
              this.closeRegisterDrawer();
            })
            .catch(error => {
              // Registration failed, handle the error
              console.error('Error during registration:', error);
              this.$message.error('Registration failed. Please try again.');
            });
        }
      });
    },

    closeRegisterDrawer() {
      this.$refs.registerForm.resetFields();
      this.registerDrawerVisible = false;
    },
    beforeRegisterDrawerClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    }
  }
};
</script>